require "view"
ui={}
local ui=ui

function ui.objectUnderMouse(objects)
  local mx,my=love.mouse.getPosition()
  local candidates,zscores={},{}
  for i, object in ipairs(objects) do
    local x,y,z,p=view.project(object.position)
    local r=p*object.radius
    --love.graphics.circle("line",x,y,r,20)
    if (mx-x)*(mx-x)+(my-y)*(my-y)<r*r then 
      table.insert(candidates, object) 
      zscores[object]=z
    end
  end
  table.sort(candidates, function(a,b) return zscores[a]>zscores[b] end)
  return candidates[1]
end

function button(text,x,y)
  local width, height=love.graphics.getFont():getWidth(text), love.graphics.getFont():getHeight()
  local mx,my=love.mouse.getPosition()
  local mouse=math.abs(mx-x)<width and math.abs(my-y)<height
  love.graphics.setColor(200,200,200)
  if mouse then love.graphics.setColor(255,255,255) end
  love.graphics.print(text, x-width/2, y-height/2)
  return mouse and love.im.mousePressed
end
